# Maintainer: Antonio Rojas <arojas@archlinux.org>

pkgname=sagemath-doc
pkgver=9.8
pkgrel=1
pkgdesc='HTML documentation for SageMath'
arch=(any)
url='http://www.sagemath.org'
license=(GPL)
depends=(python-pplpy)
makedepends=(sagemath python-pyzmq python-docutils python-jupyter_client python-pkgconfig cython
             python-jupyter-sphinx mathjax python-sphinx-furo)
source=(https://github.com/sagemath/sage/archive/$pkgver/sagemath-$pkgver.tar.gz
        sagemath-gap-4.12.patch
        sagemath-sphinx-6.patch)
sha256sums=('2aff28bd1d18c2d526581f5298acb8336f5b92db5675a7403dec8eaf9a86bc4c'
            '43dda8c7a8f9331155bdb831cdeb419953ddcb9b72d71d7c1f84f22530e753da'
            'f2dc6b43a36822412ea77eed193d182602418e0ba4669f6a5fa21985a217f4dc')
options=(!strip) # nothing to strip, save packaging time

prepare() {
  cd sage-$pkgver

# Fix tests with GAP 4.12
  patch -p1 < ../sagemath-gap-4.12.patch
# Fix build with Sphinx 6
  patch -p1 < ../sagemath-sphinx-6.patch

# https://trac.sagemath.org/ticket/25833#comment:98
  mkdir mathjax-tmp
  cp -r /usr/share/mathjax mathjax-tmp

  SAGE_ROOT="$PWD" PATH="$PWD/build/bin:$PATH" src/doc/bootstrap
  make -C src/doc doc-src

  rm -r src/sage # Use the installed sage, not the one from the source tree
}

build() {
  cd sage-$pkgver/src

  export SAGE_SRC="$PWD" \
         SAGE_DOC="$PWD"/doc \
         MATHJAX_DIR="$srcdir"/sage-$pkgver/mathjax-tmp \
         SAGE_NUM_THREADS=10 \
         LANGUAGE=C
  python -m sage_docbuild --no-pdf-links --mathjax all html
}

package() {
  cd sage-$pkgver/src/doc
 
  mkdir -p "$pkgdir"/usr/share/doc/sage
  cp -r common "$pkgdir"/usr/share/doc/sage
  cp -r html "$pkgdir"/usr/share/doc/sage

# Replace duplicated files by symlinks (Gentoo)
  cd "$pkgdir"/usr/share/doc/sage
  mv html/en/_static{,.tmp}
  for _dir in `find -name _static` ; do
    rm -r $_dir
    ln -s /usr/share/doc/sage/html/en/_static $_dir
  done
  mv html/en/_static{.tmp,}
}
